package com.wzq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.dao.dto.SpaceDto;
import com.wzq.dao.pojo.*;
import com.wzq.service.IAchievementService;
import com.wzq.service.IProjectService;
import com.wzq.service.ISpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/achievement")
public class AchievementController {

    @Autowired
    private IAchievementService iAchievementService;

    @Autowired
    private IProjectService iProjectService;

    //   添加
    @PostMapping("/{p_id}/{ascription}")
    public Result save(@PathVariable Integer p_id , @PathVariable Integer ascription) {
        Project p = iProjectService.getById(ascription);
        Achievement achievement = new Achievement();
        achievement.setP_id(p_id);
        achievement.setAscription(ascription);
        achievement.setTitle(p.getTitle());
        boolean flag = iAchievementService.save(achievement);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //查询文章所属成果
    @GetMapping("getAchievement")
    public Result getAchievement(Integer ascription){
        LambdaQueryWrapper<Achievement> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Achievement::getP_id,ascription);
        List<Achievement> list = iAchievementService.list(wrapper);
        if (list.isEmpty()){
            return new Result(Code.GET_ERR,"查询为空");
        }
        return new Result(Code.GET_OK,list);
    }

    //   修改
    @PutMapping
    public Result update(@RequestBody Achievement achievement) {
        boolean flag = iAchievementService.updateById(achievement);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iAchievementService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }


    //    获得id对应用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        LambdaQueryWrapper<Achievement> lqw = new LambdaQueryWrapper<>();
        lqw.in(Achievement::getP_id,id);
        List<Achievement> achievements = iAchievementService.list(lqw);
        Integer code = achievements != null ? Code.GET_OK : Code.GET_ERR;
        String msg = achievements != null ? "" : "数据查询失败，请重试！";
        return new Result(code,achievements,msg);
    }


    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Achievement> list = iAchievementService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

}
